/*
 * app_api.c
 *
 *  Created on: 2023年9月16日
 *      Author: zlw
 */
#include "app_api.h"
#include "net_aseq.h"
#include "device.h"
#include "app_log.h"
#include "softwareMultiTimer.h"
#include "net_protocol.h"
#include "app_bl0942.h"

#define TEST_MODE_TIMEOUT       (5*60*32768)

/************************key value handler**************************/

app_key_value_err_e SM_key_value_handler(void)
{
  app_key_value_err_e ret = 0x00;
  keyValue_t inputKeyValue ={0x00};
  uint8_t i=0;

  while((ret = get_key_value(&inputKeyValue)) != APP_KEY_VALUE_ERR_NO_DATA) {
      if(ret == APP_KEY_VALUE_NO_ERR) {
          DS_app_log_error("keyValueId = 0x%02x  data:", inputKeyValue.id);
          for(i=0;i<inputKeyValue.valueSize;i++)
            DS_app_log_error(" 0x%02x", inputKeyValue.value[i]);
          DS_app_log_error("\r\n");
          if(inputKeyValue.id == APP_KEY_VALUE_ID_COMLED_CTRL && inputKeyValue.valueSize == LEDCTRL_LENGTH) {
              if(!check_aseq(ASEQ_LED, 0, inputKeyValue.value[0]) || 0 == inputKeyValue.value[0]) {
                  if(inputKeyValue.value[0] != 0)
                    set_aseq(ASEQ_LED, inputKeyValue.value[0]);
                  SM_led_start(inputKeyValue.value+1, LED_MODEL_BLINK);
              }
          }
          else if(inputKeyValue.id == APP_KEY_VALUE_ID_OPTION && inputKeyValue.valueSize == OPTION_LENGTH) {
              recv_option_key_value_handler(inputKeyValue.value);
          }
          else if(inputKeyValue.id == APP_KEY_VALUE_ID_OPERATE_CTRL && inputKeyValue.valueSize == OPERATECTRL_LENGTH) {
              if(!check_aseq(ASEQ_OPERATE, 0, inputKeyValue.value[0]) || 0 == inputKeyValue.value[0]) {
                  if(inputKeyValue.value[0] != 0)
                    set_aseq(ASEQ_OPERATE, inputKeyValue.value[0]);
                  recv_operation_key_value_handler(inputKeyValue.value+1);
              }
          }
      }
  }
  return ret;
}

__WEAK void recv_option_key_value_handler(uint8_t *option)
{
  (void)option;
}

__WEAK void recv_operation_key_value_handler(uint8_t *operation)
{
  (void)operation;
}

__WEAK void erase_device_data(void)
{
}

/**************************test gpio************************************/

#if defined(SM_TEMPHUMI)
#define TEST_GPIO_PORT  (gpioPortC)
#define TEST_GPIO_PIN   (1)
#else
#define TEST_GPIO_PORT  (gpioPortB)
#define TEST_GPIO_PIN   (0)
#endif

static bool test_sleep = true;
MultiTimer testTimer = {.Handle = 0};

static void testTimerCallback(MultiTimer* timer, void *userData)
{
  (void)timer;
  (void)userData;
  test_sleep = true;
}

void test_gpio_level_get(void)
{
  CMU_ClockEnable(cmuClock_GPIO, true);
  GPIO_PinModeSet(TEST_GPIO_PORT, TEST_GPIO_PIN, gpioModeInputPull, 1);
  if(GPIO_PinInGet(TEST_GPIO_PORT, TEST_GPIO_PIN) == 0) {
      test_sleep = false;
      softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &testTimer, TEST_MODE_TIMEOUT, testTimerCallback,NULL,0);
  }
}

void test_model_timeout_restart(uint8_t sec)
{
  softwareMultiTimerStop(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &testTimer, 0);
  softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &testTimer, sec * 32768, testTimerCallback,NULL,0);
}

bool device_test_sleep_state_get(void)
{
  return test_sleep;
}

void rssiCheckFailedIndicate()
{
  uint8_t data[5] = {0x33,0,0,0,12};
  SM_led_start(data, FIND_ME_BLINK);
}

/******************commission handler*****************/
static MultiTimer commissionTimer = {.Handle = 0};
static uint8_t commissionCnt = 0;

static void commissionCallback(MultiTimer* timer, void *userData)
{
  static uint8_t count = 0;
  (void)userData;

  count ++;
  if(EN_MSG_STATUS_JOIN_CONFIRM_SUCCESS == get_register_status()) {
      clear_register_status();
      count = 0;
      uint8_t data[5] = {0x00};
      SM_led_start(data, NONE_BLINK);
  }
  else {
      if(count < (commissionCnt * 2))
        softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, timer, 32768, commissionCallback,NULL,0);
      else
        count = 0;
  }
}

__WEAK void startCommissiongJoin(commissionType_t type, deviceType_t device)
{
  #ifdef SM_WS
  if(type == startUpTrigger && measure_state_get() != MEASURE_NORMAL) {
      return ;
  }
#endif
  uint8_t data[5] = {0,3,0,0,60};
  if(commissionTimer.status == EN_MULTITIMER_STATUS_IDLE) {
      softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &commissionTimer, 32768, commissionCallback,NULL,0);
      if(type == startUpTrigger) {
          commissionCnt = 30;
      }
      else {
          if (device == keyFob_device || device == EB_device) {
              commissionCnt = 5;
          }
          else {
              commissionCnt = 30;
          }
      }
      data[4] = commissionCnt*2;
      Commissiong_join_node_paging_packet(commissionCnt);
      SM_led_start(data, REGIEST_BLINK);
  }
}



